package com.csaba.swing.gui.khb;

import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.csaba.connector.ServiceException;
import com.csaba.connector.khb.KHBSMSOTPValidationService;
import com.csaba.connector.model.Session;
import com.csaba.swing.action.ActionFactory;
import com.csaba.swing.action.LogoutAction;
import com.csaba.swing.gui.util.RegExpDocument;
import com.csaba.swing.gui.wizard.WizardContext;
import com.csaba.swing.gui.wizard.WizardDialog;
import com.csaba.swing.gui.wizard.WizardPage;
import com.csaba.util.I18N;

@SuppressWarnings( "serial" )
public class KHBSMSInputPage extends WizardPage
{
	private final static int SMSOTP_LENGTH = 6;

	private JTextField smsOTPField;

	@Override
	public void init( final WizardContext context )
	{
		super.init(context);

		//get session info
		final Session session = (Session) context.getProperty(KHBLoginWizardPages.PROPERTY_SESSION);

		final String loginId = session.getCustomer().getLoginId();

		add(new JLabel(I18N.getString(this, "loginId"))); //$NON-NLS-1$
		add(new JLabel(loginId)); //$NON-NLS-1$
		add(new JLabel(I18N.getString(this, "smsotp"))); //$NON-NLS-1$
		add(smsOTPField = new JTextField(new RegExpDocument("[a-zA-Z0-9]*", SMSOTP_LENGTH), "", //$NON-NLS-1$ //$NON-NLS-2$
				SMSOTP_LENGTH));

		addChangeListener(smsOTPField);
	}

	@Override
	public void updateView()
	{
		super.updateView();

		smsOTPField.setText("");
	}

	@Override
	protected void updateMessage( final Component component )
	{
		super.updateMessage(component);

		final WizardDialog dialog = context.getDialog();

		boolean ready = true;

		if ( smsOTPField.getText().length() != SMSOTP_LENGTH )
		{
			dialog.setWarning(I18N.getString(this, "msg.validSMSOTP")); //$NON-NLS-1$
			ready = false;
		}

		if ( ready )
		{
			dialog.clearMessage();
		}

		dialog.setReady(ready);
	}

	@Override
	public boolean leave( final int action )
	{
		final boolean canLeave = true;
		if ( action == ACTION_BACK || action == ACTION_CANCEL )
		{
			if ( JOptionPane.showConfirmDialog(this, "If you continue, you will loose your current session.",
					"Warning", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION )//FIXME resources
			{
				return false;
			}

			doLogout();

			context.getDialog().setReady(false);
		}
		else if ( action == ACTION_FINISH )
		{
			final String smsotp = smsOTPField.getText();

			doSMSOTPValidation(smsotp);
		}
		return canLeave;
	}

	private void doSMSOTPValidation( final String smsotp )
	{
		final Session session = (Session) context.getProperty(KHBLoginWizardPages.PROPERTY_SESSION);

		// FIXME call KHB specific SMSOTP request
		final KHBSMSOTPValidationService service = new KHBSMSOTPValidationService();
		service.setSmsotp(smsotp);

		try
		{
			service.execute(session);
		}
		catch ( final ServiceException e )
		{
			e.printStackTrace();
		}
	}

	private void doLogout()
	{
		final Session session = (Session) context.getProperty(KHBLoginWizardPages.PROPERTY_SESSION);

		//call generic logout service and remove session from GUIContext
		final LogoutAction action = (LogoutAction) ActionFactory.getAction(LogoutAction.class);
		action.doLogout(session);
	}

}
